//
//  LJCMusicSwitch.m
//  WZLProduct
//
//  Created by 陆静聪 on 16/3/23.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "LJCMusicSwitch.h"

@interface LJCMusicSwitch ()

@property (nonatomic, retain) UIButton *onOrOffButton;
@property (nonatomic, retain) UIButton *openButton;

@end

@implementation LJCMusicSwitch

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


+ (LJCMusicSwitch *)shareMusicSwitch {
    static LJCMusicSwitch *musicSwitch = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        musicSwitch = [[LJCMusicSwitch alloc] init];
        [[UIApplication sharedApplication].keyWindow addSubview:musicSwitch];
        [musicSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.offset(-20);
            make.size.mas_equalTo(CGSizeMake(200, 40));
        }];
        musicSwitch.backgroundColor = [UIColor colorWithRed:0.22 green:0.22 blue:0.24 alpha:0.5];
        
        musicSwitch.onOrOffButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [musicSwitch addSubview:musicSwitch.onOrOffButton];
        [musicSwitch.onOrOffButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.offset(0);
            make.width.mas_equalTo(40);
        }];
        musicSwitch.onOrOffButton.backgroundColor = [UIColor clearColor];
        [musicSwitch.onOrOffButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang.png"] forState:UIControlStateNormal];
        
        musicSwitch.openButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [musicSwitch addSubview:musicSwitch.openButton];
        [musicSwitch.openButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.offset(0);
            make.right.equalTo(musicSwitch.onOrOffButton.mas_left).offset(0);
        }];
        musicSwitch.openButton.backgroundColor = [UIColor clearColor];
        musicSwitch.openButton.titleLabel.font = FONT15;
                
    });
    
    
    return musicSwitch;
    
}

- (void)onOrOffMusic {
    [[LJCMusicSwitch shareMusicSwitch].onOrOffButton addTarget:self action:@selector(onOrOffAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self isOn];
    
}

- (void)onOrOffAction:(UIButton *)onOrOff {
    
    [[LJCMusicPlay shareMusicPlayer] onOrOff];
    [self isOn];
    
    
    
}

/** 开关状态判断 */
- (void)isOn {
    if ([LJCMusicPlay shareMusicPlayer].isPlay == NO) {
        [[LJCMusicSwitch shareMusicSwitch].onOrOffButton setImage:[UIImage imageNamed:@"iconfont-zanting.png"] forState:UIControlStateNormal];
    } else {
        [[LJCMusicSwitch shareMusicSwitch].onOrOffButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang.png"] forState:UIControlStateNormal];
    }

}

/** 获取标题 */
- (void)getTitle:(NSString *)title {
    [[LJCMusicSwitch shareMusicSwitch].openButton setTitle:title forState:UIControlStateNormal];
}

/** 打开播放器 */
- (void)openMusic {
    [[LJCMusicSwitch shareMusicSwitch].openButton addTarget:self action:@selector(openAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)openAction:(UIButton *)open {

    _openBlock(_urlArr, *(_index));
}

/** 添加手势 */
- (void)panPlayer {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [[LJCMusicSwitch shareMusicSwitch] addGestureRecognizer:pan];
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
    CGPoint p = [pan translationInView:[LJCMusicSwitch shareMusicSwitch]];
    [LJCMusicSwitch shareMusicSwitch].transform = CGAffineTransformTranslate([LJCMusicSwitch shareMusicSwitch].transform, p.x, p.y);
    [pan setTranslation:CGPointZero inView:[LJCMusicSwitch shareMusicSwitch]];

}


@end
